﻿using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Threading;
using System.Text.RegularExpressions;

namespace WOWMultiPlay
{
    public static class Helper_Profile
    {
        public class Profile
        {
            public Profile(string ProfileName, Button[] Buttons)
            {
                this.ProfileName = ProfileName;
                this.Buttons = Buttons;
            }

            public class Button
            {
                public Button(string ButtonName, string KeyQuery, int Repeat, TimeInterval RepeatInterval)
                {
                    this.ButtonName = ButtonName;
                    this.KeyQuery = KeyQuery;
                    this.Repeat = Repeat;
                    this.RepeatInterval = RepeatInterval;
                }

                public class TimeInterval
                {
                    public TimeInterval(string TimeIntervalStr)
                    {
                        Regex TimeIntervalRegex = new Regex("(\\d{1,})(\\D{1,})");
                        MatchCollection TimeIntervalMatches = TimeIntervalRegex.Matches(TimeIntervalStr);

                        if (TimeIntervalMatches.Count > 0)
                        {
                            for (int i = 0; i < TimeIntervalMatches.Count; i++)
                            {
                                switch (TimeIntervalMatches[i].Groups[2].Value)
                                {
                                    case "ms":
                                        this.Millisecond = Convert.ToInt32(TimeIntervalMatches[i].Groups[1].Value);
                                        break;
                                    case "s":
                                        this.Second = Convert.ToInt32(TimeIntervalMatches[i].Groups[1].Value);
                                        break;
                                    case "m":
                                        this.Minute = Convert.ToInt32(TimeIntervalMatches[i].Groups[1].Value);
                                        break;
                                    case "h":
                                        this.Hour = Convert.ToInt32(TimeIntervalMatches[i].Groups[1].Value);
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                    }

                    public int Millisecond;
                    public int Second;
                    public int Minute;
                    public int Hour;
                }

                public string ButtonName;
                public string KeyQuery;
                public int Repeat;
                public TimeInterval RepeatInterval;
                public Thread SendKeysThread;
            }

            public string ProfileName;
            public Button[] Buttons;
        }

        public static Profile LoadProfile(string ProfileFilePath)
        {
            Helper_FileEncoding.CheckFileEncodingAndChange(ProfileFilePath, Setting_Global.DefaultIniFileEncoding);

            string ProfileName = Path.GetFileName(ProfileFilePath);
            string[] ButtonsName = Helper_INIFile.IniGetAllSections(ProfileFilePath);

            if (ButtonsName.Length > 0)
            {
                Profile.Button[] ProfileButtons = new Profile.Button[ButtonsName.Length];

                for (int i = 0; i < ButtonsName.Length; i++)
                {
                    string ButtonName = ButtonsName[i];
                    string KeyQuery = Helper_INIFile.IniReadValue(ButtonsName[i], "KeyQuery", ProfileFilePath);
                    int Repeat = Convert.ToInt32(Helper_INIFile.IniReadValue(ButtonsName[i], "Repeat", ProfileFilePath));
                    Profile.Button.TimeInterval RepeatInterval = new Profile.Button.TimeInterval(Helper_INIFile.IniReadValue(ButtonsName[i], "RepeatInterval", ProfileFilePath));
                    Profile.Button ProfileButton = new Profile.Button(ButtonName, KeyQuery, Repeat, RepeatInterval);
                    ProfileButtons[i] = ProfileButton;
                }

                Profile Profile = new Profile(ProfileName, ProfileButtons);

                return Profile;
            }
            else
            {
                return null;
            }
        }

        public static void CreateProfileExample(string ProfileFilePath)
        {
            Helper_INIFile.IniWriteValue("D", "KeyQuery", "{VK_1,D}{VK_2,D}", ProfileFilePath);
            Helper_INIFile.IniWriteValue("D", "Repeat", "1", ProfileFilePath);
            Helper_INIFile.IniWriteValue("D", "RepeatInterval", "0ms", ProfileFilePath);

            Helper_INIFile.IniWriteValue("DU", "KeyQuery", "{VK_1,DU}{VK_2,DU}", ProfileFilePath);
            Helper_INIFile.IniWriteValue("DU", "Repeat", "1", ProfileFilePath);
            Helper_INIFile.IniWriteValue("DU", "RepeatInterval", "0ms", ProfileFilePath);

            Helper_INIFile.IniWriteValue("Repeat 0, 1s", "KeyQuery", "{VK_1,D}{VK_2,D}", ProfileFilePath);
            Helper_INIFile.IniWriteValue("Repeat 0, 1s", "Repeat", "0", ProfileFilePath);
            Helper_INIFile.IniWriteValue("Repeat 0, 1s", "RepeatInterval", "1s", ProfileFilePath);

            Helper_INIFile.IniWriteValue("Repeat 10, 500ms", "KeyQuery", "{VK_1,D}{VK_2,D}", ProfileFilePath);
            Helper_INIFile.IniWriteValue("Repeat 10, 500ms", "Repeat", "10", ProfileFilePath);
            Helper_INIFile.IniWriteValue("Repeat 10, 500ms", "RepeatInterval", "500ms", ProfileFilePath);
        }
    }
}